package com.bhs.sansonglogistics.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.event.Event;
import com.bhs.sansonglogistics.ui.order.OrderDetailsActivity;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;

/**
 * 抢单弹窗
 */
public class PreemptDialog extends Dialog implements View.OnClickListener {

    private ImageView mIvOrderGrabbingAnimation;
    private TextView mTvPreemptStatus;
    private Button mBtnCheckOrder;
    private Button mBtnBack;
    public String deliver_sn;
    public boolean isWinningOrder;

    public PreemptDialog(@NonNull Context context) {
        super(context, R.style.MyBottomDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preempt);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = MyUtils.getScreenWidth(getContext()) * 8 / 10;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp); //将属性设置给窗体
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        mIvOrderGrabbingAnimation = findViewById(R.id.iv_order_grabbing_animation);
        mTvPreemptStatus = findViewById(R.id.tv_preempt_status);
        mBtnCheckOrder = findViewById(R.id.btn_check_order);
        mBtnBack = findViewById(R.id.btn_back);

        mBtnBack.setOnClickListener(this);
        mBtnCheckOrder.setOnClickListener(this);
        mTvPreemptStatus.setText("抢单中...");
        mBtnCheckOrder.setVisibility(View.GONE);
        mBtnBack.setVisibility(View.GONE);
        Glide.with(getContext()).load(R.mipmap.ic_winning_order).into(mIvOrderGrabbingAnimation);
    }

    public void winningOrder() {
        isWinningOrder = true;
        mTvPreemptStatus.setText("抢单中...");
        mBtnCheckOrder.setVisibility(View.GONE);
        mBtnBack.setVisibility(View.GONE);
        Glide.with(getContext()).load(R.mipmap.ic_winning_order).into(mIvOrderGrabbingAnimation);
    }


    public void succeed(int deliver_type) {
        if (deliver_type == 3) {
            show();
        }
        LogUtils.d("抢单成功");
        isWinningOrder = false;
        Glide.with(getContext()).load(R.mipmap.ic_received_success).into(mIvOrderGrabbingAnimation);
        mTvPreemptStatus.setText(deliver_type != 3 ? "抢单成功" : "报价接单成功");
        mBtnCheckOrder.setVisibility(View.VISIBLE);
        mBtnBack.setVisibility(View.VISIBLE);
    }

    public void failure() {
        isWinningOrder = false;
        Glide.with(getContext()).load(R.mipmap.ic_preempt_fail).into(mIvOrderGrabbingAnimation);
        mTvPreemptStatus.setText("抢单失败");
        mBtnCheckOrder.setVisibility(View.VISIBLE);
        mBtnCheckOrder.setText("确认");
    }

    public void overtime() {
        Glide.with(getContext()).load(R.mipmap.ic_preempt_fail).into(mIvOrderGrabbingAnimation);
        mTvPreemptStatus.setText("网络错误");
        mBtnCheckOrder.setVisibility(View.VISIBLE);
        mBtnCheckOrder.setText("确认");
    }

    public void error(String msg) {
        Glide.with(getContext()).load(R.mipmap.ic_preempt_fail).into(mIvOrderGrabbingAnimation);
        mTvPreemptStatus.setText(msg);
        mBtnCheckOrder.setVisibility(View.VISIBLE);
        mBtnCheckOrder.setText("确认");
    }

    private MyClickListener2 listener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.listener = l;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_check_order) {
            listener.onClick(mBtnCheckOrder.getText().toString(), null);
        } else {
            listener.onClick(mBtnBack.getText().toString(), null);
        }
        mTvPreemptStatus.setText("抢单中...");
        mBtnCheckOrder.setVisibility(View.GONE);
        mBtnBack.setVisibility(View.GONE);
        Glide.with(getContext()).load(R.mipmap.ic_winning_order).into(mIvOrderGrabbingAnimation);
    }
}
